package cn.springcloud.fix.novel.biz.controller;

import cn.springcloud.fix.novel.biz.service.AsyncTestService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;

@Slf4j
@RestController
@AllArgsConstructor
public class AsyncTestController {

    private AsyncTestService ansyncTestService;

    @GetMapping("/{name}")
    public String test(@PathVariable String name) {
        return ansyncTestService.requestAnsync(name);
    }

    @GetMapping("/n/{name}")
    public String noReturn(@PathVariable String name) {
        return ansyncTestService.noReturn(name);
    }

    @GetMapping("/ng/{name}")
    public String noGet(@PathVariable String name) {
        return ansyncTestService.noGet(name);
    }

    @GetMapping("/books")
    public String books() {
        File file = new File("/books/星辰变/第八章 胆识.txt");
        BufferedReader reader = null;
        String tempString;
        try {
            StringBuilder urlString = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
            while ((tempString = reader.readLine()) != null) {
                urlString.append(tempString);
            }
            reader.close();
            return urlString.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "error";
        // URL url = new URL("http://localhost/books/%E6%98%9F%E8%BE%B0%E5%8F%98/%E7%AC%AC%E5%85%AB%E7%AB%A0%20%E8%83%86%E8%AF%86.txt");   //想要读取的url地址
        //
        // URLConnection conn = url.openConnection();
        // BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        //
        // StringBuilder urlString = new StringBuilder();
        // String current;
        // while ((current = in.readLine()) != null) {
        //     urlString.append(current);
        // }

        // return urlString.toString();
        //写到本地
        // File fp = new File("D:/test.java");
        // OutputStream os = new FileOutputStream(fp);
        // os.write(urlString.toString().getBytes());
        // os.close();

        //     } catch(Exception e){
        //         e.printStackTrace();
        //     }
        //     return "error";
        // }
    }
}